//
//  WKEliteViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-2.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKOneEliteViewController.h"
#import "WKHomeHttpTool.h"
#import "WKOneViewCell.h"
#import "WKDetailViewController.h"
#import "WKStatusFrame.h"


@interface WKOneEliteViewController ()
/**frame模型数组*/
@property (nonatomic,strong) NSMutableArray *statusFrames;

@end

@implementation WKOneEliteViewController

#pragma mark - 懒加载
- (NSMutableArray *)statusFrames
{
    if (_statusFrames == nil) {
        _statusFrames = [NSMutableArray array];
    }
    return _statusFrames;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //加载最新数据
    [self refreshData];

    //监听刷新按钮点击的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:WKDidDeletedNotificationOne object:nil];
}
/**
 *  上拉刷新
 */
- (void)loadMoreData
{
    WKHomeParam *param = [[WKHomeParam alloc] init];
    
    WKStatusFrame *lastStatusFrame = self.statusFrames.lastObject;
    WKResult *lastStatus = lastStatusFrame.result;
    
    if (lastStatus) {
        // max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0
        param.maxid = [NSString stringWithFormat:@"%lld",[lastStatus.id longLongValue]+1];
    }

    [WKHomeHttpTool getEliteMoreDataListWithParam:param success:^(WKHomeResult *result) {
        //取出微博模型数组
        NSArray *statusesDict = [self statusFrameWithStatuses:result.homeDatas];
        
        //将新数据插入到旧数据的后面
        [self.statusFrames addObjectsFromArray:statusesDict];
        
        
        //刷新数据
        [self.tableView reloadData];
        
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
    } failure:^(NSError *error) {
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
    }];
    
//    WKLog(@"loadMoreData");

}
/**
 *  下拉刷新
 */
- (void)refreshData
{
    [self.tableView headerBeginRefreshing];
    [self loadNewData];
}

- (void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (NSMutableArray *)statusFrameWithStatuses:(NSArray *)statuses
{
    NSMutableArray *frames = [NSMutableArray array];
    
      for (WKResult *result in statuses) {
        WKStatusFrame *frame = [[WKStatusFrame alloc] init];
        //传递微博模型数据，计算所有子控件的frame
        frame.result = result;
        [frames addObject:frame];
    }
    return frames;
    
}
/**
 *加载最新数据
 */
- (void)loadNewData
{
    WKHomeParam *param = [[WKHomeParam alloc] init];
//    param.count = @"10";
    [WKHomeHttpTool getEliteNewDataListWithParam:param success:^(WKHomeResult *result) {
        //如果返回数据为空则返回;
        if (result == nil) return;
        self.statusFrames = [self statusFrameWithStatuses:result.homeDatas];
        [MBProgressHUD hideHUD];
        //显示底部刷新控件
        self.tableView.footerHidden = NO;
        //刷新数据
        [self.tableView reloadData];
        
        //停止下拉刷新
        [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {

        //停止下拉刷新
        [self.tableView headerEndRefreshing];
        WKLog(@"%@",error);
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statusFrames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WKOneViewCell *cell = [WKOneViewCell cellWithTableView:tableView];

    cell.statusFrame = self.statusFrames[indexPath.row];
    return cell;
}

#pragma mark - tableView的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //动画取消cell选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WKStatusFrame *statusFrame = self.statusFrames[indexPath.row];

    WKDetailViewController *dtVc = [[WKDetailViewController alloc] init];
    dtVc.statusFrame = statusFrame;

    [self.naVc pushViewController:dtVc animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    WKStatusFrame *statusFrame = self.statusFrames[indexPath.row];
    
    return statusFrame.cellHeigth;
}

@end
